
import hashlib, base64
import json
import random

import requests

from application import app


class UserService(object):

    # 登录密码加密
    @staticmethod
    def genePwd(pwd, salt):
        m = hashlib.md5()
        str = '%s-%s' % (base64.encodebytes(pwd.encode('utf-8')), salt)
        m.update(str.encode('utf-8'))
        return m.hexdigest()

    # cookie加密
    @staticmethod
    def geneAuthCode(user_info):
        m = hashlib.md5()
        str = '%s-%s-%s-%s' % (user_info.uid, user_info.login_name, user_info.login_pwd, user_info.login_salt)
        m.update(str.encode('utf-8'))
        return m.hexdigest()

    @staticmethod
    def geneSalt():
        seed = "1234567890abcdefghijklmnopqrstuvwxyz"
        sa = []
        for i in range(15):
            sa.append(random.choice(seed))
        salt = ''.join(sa).upper()
        return salt